/**
 * Created by XianTao on 2016/11/12
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.biz.wechat.lechuan.base.service;

import org.lego.biz.wechat.lechuan.base.entity.UserPrize;
import org.lego.biz.wechat.lechuan.base.repository.UserPrizeRepository;
import org.lego.jpa.base.repository.BaseRepository;
import org.lego.jpa.base.service.BaseService;
import org.lego.jpa.base.util.DynamicSpecifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserPrizeService extends BaseService<UserPrize, String> {
    @Autowired
    private UserPrizeRepository userPrizeRepository;

    protected BaseRepository<UserPrize, String> getRepository() {
        return userPrizeRepository;
    }

    /**
     * 统计图文已领礼品数 (ISNOTNULL_openId)
     *
     * @param newsId 图文id
     * @return
     */
    public Integer count(String newsId) {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_pushNews.id", newsId);
        searchParams.put("ISNOTNULL_openId", "null");
        return new Long(this.count(DynamicSpecifications.buildSpecification(searchParams, UserPrize.class))).intValue();
    }

    /**
     * 根据用户openId和图文id，查询一条用户礼品记录
     *
     * @param openId 用户openId
     * @param newsId 图文id
     * @return
     */
    public UserPrize findOneByOpenIdAndNewsId(String openId, String newsId) {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_openId", openId);
        searchParams.put("EQ_pushNews.id", newsId);
        return this.findOne(DynamicSpecifications.buildSpecification(searchParams, UserPrize.class));
    }

    /**
     * 查询一条已领礼品记录 (ISNOTNULL_openId)
     *
     * @param telephone
     * @param newsId
     * @return
     */
    public UserPrize findOneByTelephoneAndNewsId(String telephone, String newsId) {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_telephone", telephone);
        searchParams.put("EQ_pushNews.id", newsId);
        searchParams.put("ISNOTNULL_openId", "null");
        return this.findOne(DynamicSpecifications.buildSpecification(searchParams, UserPrize.class));
    }

    /**
     * 查询未领礼品记录列表 (ISNULL_openId)
     *
     * @param prizeCode
     * @param telephone
     * @return
     */
    public List<UserPrize> findByPrizeCodeAndTelephone(String prizeCode, String telephone) {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_prizeCode", prizeCode);
        searchParams.put("EQ_telephone", telephone);
        searchParams.put("ISNULL_openId", "null");
        return this.findAll(DynamicSpecifications.buildSpecification(searchParams, UserPrize.class));
    }

    /**
     * 查询未领礼品记录列表
     *
     * @param prizeCode
     * @param telephone
     * @return
     */
    public List<UserPrize> findByPrizeCodeAndTelephone2(String prizeCode, String telephone) {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_prizeCode", prizeCode);
        searchParams.put("EQ_telephone", telephone);
        return this.findAll(DynamicSpecifications.buildSpecification(searchParams, UserPrize.class));
    }
}
